package com.jvup.gameshow.model.viewModel.impl;

import android.view.View;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
import androidx.recyclerview.widget.RecyclerView.Adapter;

import com.jvup.common.support.base.BaseViewModel;
import com.jvup.gameshow.BR;
import com.jvup.gameshow.R;
import com.jvup.gameshow.databinding.LayoutShareIdeaPictureBinding;
import com.jvup.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.jvup.common.widget.adapter.auto.AutoVariable;
import com.jvup.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.jvup.gameshow.view.attributeBinder.attribute.ImageAttribute;
import com.qire.antscore.common.AssertUtils;
import com.qire.antsrouter.lifecycleHandler.ActivityManageHandler;

import java.util.ArrayList;
import java.util.LinkedList;

public class ShareIdeaViewModel extends BaseViewModel<ShareIdeaViewModel> {

    private final int MAX_COUNT = 9;
    private final LinkedList<ShareIdeaPictureItemViewModel> pictureItemViewModelList = new LinkedList<>();
    private final ShareIdeaPictureItemViewModel addPictureBtnItem = new ShareIdeaPictureItemViewModel();
    private final SummerRecyclerBindAdapter adapter = new SummerRecyclerBindAdapter(pictureItemViewModelList);

    public ShareIdeaViewModel() {
        resetPictureItemList();
    }

    @Override
    public int getVariableId() {
        return BR.shareIdeaViewModel;
    }

    /**
     * 重置图片列表
     */
    public void resetPictureItemList(){
        pictureItemViewModelList.clear();
        pictureItemViewModelList.add(addPictureBtnItem);
    }

    /**
     * 剩余可选择照片的数量，最大限制数-已选择数+1 包含了按钮项
     * @return
     */
    public int remainingCount() {
        return (MAX_COUNT - pictureItemViewModelList.size()) + 1;
    }

    /**
     * 删除某张图片
     * @param itemViewModel 图片Item的对象
     */
    public void deletePictureItem(ShareIdeaPictureItemViewModel itemViewModel) {
        int position = pictureItemViewModelList.indexOf(itemViewModel);
        pictureItemViewModelList.remove(itemViewModel);
        adapter.notifyItemRemoved(position);
        addPictureBtnItem.notifyChange();
        updateSubmitBtnText(remainingCount()<=0);
    }

    /**
     * 接受来自图片选择页面的回调，在选择完毕点击下一步时触发
     * @param pickerPathList 选择好的图片地址数组
     */
    public void onPickerImage(ArrayList<String> pickerPathList){
        if(AssertUtils.notEmpty(pickerPathList)) {
            for(String url : pickerPathList) {
                if(pictureItemViewModelList.size()>9) {
                    break;
                }
                pictureItemViewModelList.addFirst(new ShareIdeaPictureItemViewModel(url));
            }
            adapter.notifyDataSetChanged();
            updateSubmitBtnText(remainingCount()<=0);
        }
    }

    public void updateSubmitBtnText(boolean isInvalid){
        put("submitBtnTextClickable",isInvalid);
        put("submitBtnTextColor",isInvalid ? "#ff7c46" : "#aeaeae");
    }

    public MutableLiveData<Boolean> getSubmitBtnTextClickable() {
        return get("submitBtnTextClickable");
    }

    public MutableLiveData<String> getSubmitBtnTextColor() {
        return get("submitBtnTextColor");
    }

    public String getIdeaContentValue(){
        return getToValue("ideaContent");
    }

    public MutableLiveData<String> getIdeaContent() {
        return get("ideaContent");
    }


    /**
     * 绑定适配器事件处理器
     * @param autoVariable
     */
    public void bindEventProcess(AutoVariable autoVariable){
        adapter.setEventProcess(autoVariable);
    }

    /**
     * 图片展示RecyclerView的布局管理，这里使用3列规格的网格布局
     * @return 布局管理器
     */
    public LayoutManager getLayoutManager() {
        return new GridLayoutManager(ActivityManageHandler.HANDLER.currentActivity(),3,GridLayoutManager.VERTICAL,false);
    }

    /**
     * 图片展示RecyclerView的数据适配器
     * @return 适配器
     */
    public Adapter getPictureAdapter(){
        return adapter;
    }

    /**
     * 图片展示RecyclerView的填充项目ViewModel
     */
    @AdapterEntityBind(viewRid = R.layout.layout_share_idea_picture)
    public class ShareIdeaPictureItemViewModel extends BaseObservable implements AutoVariable<LayoutShareIdeaPictureBinding> {

        public final ImageAttribute imageAttribute;
        public final boolean isBtn;

        public ShareIdeaPictureItemViewModel() {
            this.imageAttribute = new ImageAttribute(R.drawable.app_share_idea_icon_add_picture);
            this.isBtn = true;
        }

        public ShareIdeaPictureItemViewModel(String pictureUrl) {
            this.imageAttribute = new ImageAttribute(pictureUrl);
            this.isBtn = false;
        }

        @Override
        public void bindVariable(LayoutShareIdeaPictureBinding viewBind) {
            viewBind.setShareIdeaPictureItemViewModel(this);
        }

        @Bindable
        public int getItemVisible(){
            return (isBtn && remainingCount() <= 0) ? View.GONE : View.VISIBLE;
        }

        @Bindable
        public ImageAttribute getPicture() {
            return imageAttribute;
        }

        @Bindable
        public int getDeleteBtnVisible(){
            return isBtn ? View.GONE : View.VISIBLE;
        }

    }

} 
